#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include"inc/app.h"
#include"inc/game.h"
#include"inc/game2048.h"
#include"inc/tem.h"
#include"inc/pthread.h"

lv_obj_t* btn;
lv_obj_t* screen;
pthread_mutex_t ui_mutex;
extern lv_obj_t* app_container;  //软件盒界面容器
int appui;


extern lv_obj_t *temp_label;
extern lv_obj_t *humi_label;
extern lv_obj_t *press_label;
extern lv_obj_t *alti_label;
extern lv_obj_t *light_label;
void appstart(lv_event_t *e)
{    
   //lv_obj_del(btn);
   
   if(!appui)
    {create_app(screen); //创建软件盒界面
       appui=1;
    }     
    lv_obj_remove_flag(app_container, LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(btn, LV_OBJ_FLAG_HIDDEN);
  
}
extern const lv_font_t lv_mygec_font;
void create_appstart(lv_obj_t* parent)//初始界面
{
  //  lv_obj_t * win = lv_obj_create(lv_screen_active());
    // 创建按钮容器
    btn = lv_btn_create(parent);
    lv_obj_set_size(btn, 100, 120);
    lv_obj_set_style_bg_color(btn, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
    lv_obj_set_style_radius(btn, 10, LV_PART_MAIN);
    lv_obj_set_align(btn,LV_ALIGN_CENTER);

    // 应用图标
    lv_obj_t* icon = lv_img_create(btn);
    lv_img_set_src(icon, "A:/tzh/app.bmp");
   
    // 应用名称
    lv_obj_t* label = lv_label_create(btn);
    lv_label_set_text(label, "软件盒子");
    //lv_obj_set_style_text_font(label, &lv_mygec_font, LV_PART_MAIN);
    lv_obj_set_style_text_font(label, &lv_mygec_font, 0);
    lv_obj_set_style_text_color(label, lv_color_hex(0x000000), 0);
    lv_obj_align(label, LV_ALIGN_BOTTOM_MID, 0, -15);

    // 添加点击事件回调
    lv_obj_add_event_cb(btn, appstart, LV_EVENT_CLICKED, NULL);
  
}

int game_ui_requested;
int game2_ui_requested;
int tem_update_requested ;
int main(void)
{
    //1、设备初始化
    lv_init();
    pthread_mutex_init(&ui_mutex, NULL);// 初始化互斥锁
    /*Linux frame buffer device init*/
    lv_display_t * disp = lv_linux_fbdev_create();
    lv_linux_fbdev_set_file(disp, "/dev/fb0");
    lv_indev_t * indev = lv_evdev_create(LV_INDEV_TYPE_POINTER,"/dev/input/event0");
    //  2、主要操作 
    screen = lv_scr_act();
     appui=0;
    //create_app(screen);      //创建软件盒界面
    create_appstart(screen); // 进入软件盒
    game_ui_requested=0;
    tem_update_requested =0;
     game2_ui_requested=0;

    while(1) 
    {
        lv_timer_handler();
       
        //read_sensor_data();
        if(game_ui_requested==1)
        {
            pthread_cancel(tid1);
         
            game_ui_requested=0;
        }
        if(tem_update_requested ==1)
        {
            pthread_cancel(tid3);
           
           tem_update_requested =0;
        }
        if(game2_ui_requested ==1)
        {
            pthread_cancel(tid2);
           
           game2_ui_requested =0;
        }
       
    usleep(5000);
   }
    pthread_mutex_destroy(&ui_mutex);
    return 0;
}
